package com.yunfeng.aolin.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestParam;

import com.yunfeng.common.log.Log;
import com.yunfeng.common.log.LogMessageObject;
import com.yunfeng.common.log.impl.LogUitl;
import com.yunfeng.common.util.FileUtils;
import com.yunfeng.common.util.AjaxObject;
import com.yunfeng.common.util.Page;
import com.yunfeng.aolin.entity.OrderScheduling;
import com.yunfeng.aolin.service.OrderSchedulingService;

@Controller
@RequestMapping("/management/orders/orderscheduling")
public class OrderSchedulingController {

	@Autowired
	private OrderSchedulingService orderSchedulingService;
	
	private static final String CREATE = "management/orders/orderscheduling/create";
	private static final String UPDATE = "management/orders/orderscheduling/update";
	private static final String LIST = "management/orders/orderscheduling/list";
	private static final String VIEW = "management/orders/orderscheduling/view";
	
	@InitBinder
	public void dataBinder(WebDataBinder dataBinder) {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		dataBinder.registerCustomEditor(Date.class, 
				new CustomDateEditor(df, true));
	}
	
	@RequiresPermissions("OrderScheduling:save")
	@RequestMapping(value="/create", method=RequestMethod.GET)
	public String preCreate(Map<String, Object> map) {
		return CREATE;
	}
	
	@Log(message="添加了id={0}订单排程。")
	@RequiresPermissions("OrderScheduling:save")
	@RequestMapping(value="/create", method=RequestMethod.POST)
	public @ResponseBody String create(@Valid OrderScheduling orderScheduling) {
		orderSchedulingService.saveOrUpdate(orderScheduling);

		LogUitl.putArgs(LogMessageObject.newWrite().setObjects(new Object[]{orderScheduling.getId()}));
		return AjaxObject.newOk("订单排程添加成功！").toString();
	}
	
	@ModelAttribute("preloadOrderScheduling")
	public OrderScheduling preload(@RequestParam(value = "id", required = false) Long id) {
		if (id != null) {
			OrderScheduling orderScheduling = orderSchedulingService.get(id);
			return orderScheduling;
		}
		return null;
	}
	
	@RequiresPermissions("OrderScheduling:edit")
	@RequestMapping(value="/update/{id}", method=RequestMethod.GET)
	public String preUpdate(@PathVariable Long id, Map<String, Object> map) {
		OrderScheduling orderScheduling = orderSchedulingService.get(id);
		map.put("orderScheduling", orderScheduling);
		return UPDATE;
	}
	
	@Log(message="修改了id={0}订单排程的信息。")
	@RequiresPermissions("OrderScheduling:edit")
	@RequestMapping(value="/update", method=RequestMethod.POST)
	public @ResponseBody String update(@Valid @ModelAttribute("preloadOrderScheduling")OrderScheduling orderScheduling) {
		orderSchedulingService.saveOrUpdate(orderScheduling);

		LogUitl.putArgs(LogMessageObject.newWrite().setObjects(new Object[]{orderScheduling.getId()}));
		return AjaxObject.newOk("订单排程修改成功！").toString();
	}

	@Log(message="删除了id={0}订单排程。")
	@RequiresPermissions("OrderScheduling:delete")
	@RequestMapping(value="/delete/{id}", method=RequestMethod.POST)
	public @ResponseBody String delete(@PathVariable Long id) {
		orderSchedulingService.delete(id);

		LogUitl.putArgs(LogMessageObject.newWrite().setObjects(new Object[]{id}));
		return AjaxObject.newOk("订单排程删除成功！").setCallbackType("").toString();
	}
	
	@Log(message="批量删除了id={0}订单排程。")
	@RequiresPermissions("OrderScheduling:delete")
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public @ResponseBody String deleteMany(Long[] ids) {
		for (int i = 0; i < ids.length; i++) {
			OrderScheduling orderScheduling = orderSchedulingService.get(ids[i]);
			orderSchedulingService.delete(orderScheduling.getId());
		}
		
		LogUitl.putArgs(LogMessageObject.newWrite().setObjects(new Object[]{Arrays.toString(ids)}));
		return AjaxObject.newOk("订单排程删除成功！").setCallbackType("").toString();
	}

	@RequiresPermissions("OrderScheduling:view")
	@RequestMapping(value="/list", method={RequestMethod.GET, RequestMethod.POST})
	public String list(Page page, String keywords, Map<String, Object> map) {
		List<OrderScheduling> orderSchedulings = null;
		if (StringUtils.isNotBlank(keywords)) {
			orderSchedulings = orderSchedulingService.findByLiaohao(page, keywords);
		} else {
			orderSchedulings = orderSchedulingService.findAll(page);
		}

		map.put("page", page);
		map.put("orderSchedulings", orderSchedulings);
		map.put("keywords", keywords);

		return LIST;
	}
	
	@RequiresPermissions("OrderScheduling:view")
	@RequestMapping(value="/view/{id}", method={RequestMethod.GET})
	public String view(@PathVariable Long id, Map<String, Object> map) {
		OrderScheduling orderScheduling = orderSchedulingService.get(id);
		map.put("orderScheduling", orderScheduling);
		return VIEW;
	}
	
	
	@RequiresPermissions("OrderScheduling:exportExcel")
	@RequestMapping(value = "/exportExcel", method = { RequestMethod.GET,
			RequestMethod.POST })
	public void exportExcel(HttpServletResponse response) throws Exception {
		List<OrderScheduling> orderSchedulings = orderSchedulingService.findAll();
		FileUtils.exportExcel(response, orderSchedulings);
	}

}
